﻿package mortal.game.view.task.view.track
{
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.controls.*;
    import flash.geom.*;
    import mortal.common.*;
    import mortal.common.net.*;
    import mortal.component.gLinkText.*;
    import mortal.component.gconst.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.view.common.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.task.data.*;
    import mortal.game.view.task.view.*;
    import mortal.game.view.task.view.render.*;
    import mortal.mvc.core.*;

    public class TaskTrackContainer extends GSprite
    {
        private var _items:Array;
        private var _render:Class;
        private var _contentContainer:GSprite;
        private var _pane:GScrollPane;
        private var _canShowHide:Boolean = false;
        private var _isShow:Boolean = true;
        private var _taskGuideArrow:TaskGuideArrow;
        private var _normalGuideArrow:GuideArrow;

        public function TaskTrackContainer()
        {
            this._items = [];
            this._render = TaskTrackBase;
            return;
        }// end function

        protected function get normalGuideArrow() : GuideArrow
        {
            if (this._normalGuideArrow == null)
            {
                this._normalGuideArrow = UICompomentPool.getUICompoment(GuideArrow) as GuideArrow;
            }
            return this._normalGuideArrow;
        }// end function

        protected function disposeNormalGuideArrow() : void
        {
            if (this._normalGuideArrow != null)
            {
                this._normalGuideArrow.dispose(true);
                this._normalGuideArrow = null;
            }
            return;
        }// end function

        protected function disposeGuideArrow() : void
        {
            if (this._taskGuideArrow != null)
            {
                this._taskGuideArrow.dispose(true);
                this._taskGuideArrow = null;
            }
            return;
        }// end function

        public function hideGuideArrow() : void
        {
            this.disposeNormalGuideArrow();
            this.disposeGuideArrow();
            return;
        }// end function

        private function itemExpandHandler(event:DataEvent) : void
        {
            CallLater.addCallBack(this.updateLayout);
            return;
        }// end function

        public function get canShowHide() : Boolean
        {
            return this._canShowHide;
        }// end function

        public function set canShowHide(param1:Boolean) : void
        {
            this._canShowHide = param1;
            return;
        }// end function

        public function getGuidingItem() : TaskTrackBase
        {
            var _loc_1:TaskTrackBase = null;
            if (this._items == null)
            {
                return null;
            }
            for each (_loc_1 in this._items)
            {
                
                if (_loc_1.isGuiding)
                {
                    return _loc_1;
                }
            }
            return null;
        }// end function

        public function showGuideArrow(param1:int, param2:int, param3:GLinkTextData, param4:Boolean, param5:Boolean = false) : void
        {
            var _loc_6:* = this.getGuidingItem();
            if (_loc_6 == null)
            {
                return;
            }
            var _loc_7:* = _loc_6.localToGlobal(GameConst.yellowPoint);
            var _loc_8:* = this.localToGlobal(GameConst.yellowPoint);
            if (param5)
            {
                this.disposeGuideArrow();
                this.showNormalGuideArrow(12, param2 + _loc_7.y - _loc_8.y + 4);
            }
            else
            {
                this.disposeNormalGuideArrow();
                this.showTaskGuideArrow(param1 + _loc_7.x - _loc_8.x, param2 + _loc_7.y - _loc_8.y, param3, param4);
            }
            return;
        }// end function

        private function showNormalGuideArrow(param1:int, param2:int) : void
        {
            this.normalGuideArrow.direction = GuideArrow.Right;
            this.normalGuideArrow.x = param1;
            this.normalGuideArrow.y = param2;
            this.normalGuideArrow.label = Language.getString(21187);
            this.addChild(this.normalGuideArrow);
            return;
        }// end function

        private function showTaskGuideArrow(param1:int, param2:int, param3:GLinkTextData, param4:Boolean) : void
        {
            if (this._taskGuideArrow == null)
            {
                this._taskGuideArrow = UICompomentPool.getUICompoment(TaskGuideArrow) as TaskGuideArrow;
            }
            this._taskGuideArrow.x = param1;
            this._taskGuideArrow.y = param2;
            this._taskGuideArrow.updateDatas(param3, param4, false);
            this.addChild(this._taskGuideArrow);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._contentContainer = UICompomentPool.getUICompoment(GSprite);
            this._pane = UIFactory.gScrollPanel(0, 0, 200, 180, this);
            this._pane.styleName = "chatScrollPane";
            this._pane.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._pane.verticalScrollPolicy = ScrollPolicy.AUTO;
            this._pane.source = this._contentContainer;
            Dispatcher.addEventListener(EventName.TaskTraceItemExpand, this.itemExpandHandler);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._contentContainer.dispose(param1);
            this._contentContainer = null;
            this._pane.dispose(param1);
            this._pane = null;
            this._canShowHide = false;
            this._isShow = false;
            DisplayUtil.delNotUse(this._items, 0, param1);
            this._items = [];
            this.hideGuideArrow();
            Dispatcher.removeEventListener(EventName.TaskTraceItemExpand, this.itemExpandHandler);
            return;
        }// end function

        public function get itemsNum() : int
        {
            if (this._items == null)
            {
                return 0;
            }
            return this._items.length;
        }// end function

        public function get bodyHeight() : int
        {
            return this._contentContainer.height;
        }// end function

        public function setScrollPaneHight(param1:int = 150) : void
        {
            this._pane.setSize(210, param1);
            this._pane.drawNow();
            return;
        }// end function

        public function set render(param1:Class) : void
        {
            this._render = param1;
            return;
        }// end function

        public function initList(param1:Array) : void
        {
            var _loc_3:TaskTrackBase = null;
            if (this._items.length > 0)
            {
                DisplayUtil.delNotUse(this._items, 0);
                this._items = [];
            }
            if (param1 == null || param1.length == 0)
            {
                return;
            }
            var _loc_2:int = 0;
            while (_loc_2 < param1.length)
            {
                
                _loc_3 = UICompomentPool.getUICompoment(this._render);
                _loc_3.setMyData(param1[_loc_2]);
                this._contentContainer.addChild(_loc_3);
                this._items.push(_loc_3);
                _loc_2++;
            }
            this.updateLayout();
            _loc_2 = 0;
            while (_loc_2 < this._items.length)
            {
                
                _loc_3 = this._items[_loc_2];
                _loc_3.updateGuide();
                if (_loc_3.isGuiding)
                {
                    this._contentContainer.setChildIndex(_loc_3, (this._contentContainer.numChildren - 1));
                    break;
                }
                _loc_2++;
            }
            return;
        }// end function

        public function removeItem(param1:int, param2:Boolean = true) : void
        {
            var _loc_3:* = this.getItem(param1);
            if (_loc_3 == null)
            {
                return;
            }
            _loc_3.dispose(true);
            var _loc_4:* = this._items.indexOf(_loc_3);
            this._items.splice(_loc_4, 1);
            if (param2)
            {
                this.updateLayout();
            }
            return;
        }// end function

        public function updateAllCopy() : void
        {
            var _loc_1:TaskTrackBase = null;
            for each (_loc_1 in this._items)
            {
                
                if (_loc_1 == null || _loc_1.copyInfo == null)
                {
                    return;
                }
            }
            return;
        }// end function

        public function updateItem(param1:TaskInfo, param2:Boolean = true) : void
        {
            this.addItem(param1, param2);
            return;
        }// end function

        public function addItem(param1:TaskInfo, param2:Boolean = true) : void
        {
            var _loc_3:* = this.getItem(param1.stask.code);
            if (_loc_3 == null)
            {
                _loc_3 = UICompomentPool.getUICompoment(this._render);
                this._contentContainer.addChild(_loc_3);
                this._items.push(_loc_3);
            }
            _loc_3.taskInfo = param1;
            if (param2)
            {
                this.updateLayout();
            }
            _loc_3.updateGuide();
            return;
        }// end function

        private function getItem(param1:int) : TaskTrackBase
        {
            var _loc_2:TaskTrackBase = null;
            for each (_loc_2 in this._items)
            {
                
                if (_loc_2.taskInfo != null && _loc_2.taskInfo.stask.code == param1)
                {
                    return _loc_2;
                }
            }
            return null;
        }// end function

        public function updateLayout() : void
        {
            var _loc_3:TaskTrackBase = null;
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            while (_loc_2 < this._items.length)
            {
                
                _loc_3 = this._items[_loc_2];
                _loc_3.x = 0;
                _loc_3.y = _loc_1;
                _loc_1 = _loc_1 + (_loc_3.height + 2);
                _loc_2++;
            }
            this._pane.source = this._contentContainer;
            this._pane.drawNow();
            return;
        }// end function

    }
}
